package com.platform.usercenter.teamfc.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.page.PageMethod;
import com.platform.common.core.domain.R;
import com.platform.common.core.domain.base.AirCargoRequest;
import com.platform.common.core.domain.entity.SysUser;
import com.platform.common.core.domain.model.LoginUser;
import com.platform.common.core.utils.StringUtils;
import com.platform.common.core.utils.sql.SqlUtil;
import com.platform.common.core.web.controller.BaseController;
import com.platform.common.core.web.page.PageDomain;
import com.platform.common.core.web.page.TableDataInfo;
import com.platform.common.core.web.page.TableSupport;
import com.platform.common.security.service.TokenService;
import com.platform.constants.SysUserConstants;
import com.platform.usercenter.domain.CalLeaderPlan;
import com.platform.usercenter.domain.SysMenu;
import com.platform.usercenter.domain.SysOrganizationRelation;
import com.platform.usercenter.domain.dto.CalLeaderPlanDTO;
import com.platform.usercenter.domain.vo.SysOrgDeptUserVO;
import com.platform.usercenter.domain.vo.SysRoleVO;
import com.platform.usercenter.domain.vo.SysUserVO;
import com.platform.usercenter.service.ISysOrganizationRelationService;
import com.platform.usercenter.service.ISysRoleService;
import com.platform.usercenter.teamfc.domain.dto.MenuDTO;
import com.platform.usercenter.teamfc.domain.dto.UserDTO;
import com.platform.usercenter.teamfc.domain.vo.SysDeptTreeVO;
import com.platform.usercenter.teamfc.service.PlatUserCenterFcService;
import com.platform.usercenter.teamfc.uti.DateUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/platUserCenterFc")
public class PlatUserCenterFcController extends BaseController {

    @Resource
    private PlatUserCenterFcService platUserCenterFcService;

    @Resource
    private TokenService tokenService;
    @Resource
    ISysOrganizationRelationService organizationRelationService;
    @Resource
    private ISysRoleService roleService;
    /**
     * 查询菜单列表
     *
     * @param menu
     * @return
     */
    @PostMapping("/menu/list")
    public R<TableDataInfo> list(@RequestBody MenuDTO menu) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = menu.getPageNum();
        Integer pageSize = menu.getPageSize();
        String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
        Boolean reasonable = pageDomain.getReasonable();
        PageMethod.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
        LoginUser loginUser = tokenService.getLoginUser();
        Long userId = loginUser.getUserid();
        Long orgId = loginUser.getOrgId();
        List<SysMenu> menus = platUserCenterFcService.selectMenuList(menu, userId, orgId);
        TableDataInfo dataTable = getDataTable(menus);
        return R.ok(dataTable);
    }

    /**
     * 查询全部用户id
     *
     * @return
     */
    @GetMapping("/user/queryUserIdList")
    public R<List<SysUser>> queryUserIdList() {
        List<SysUser> sysUserList = platUserCenterFcService.queryUserIdList(tokenService.getLoginUser().getOrgId());
        return R.ok(sysUserList);
    }

    /**
     * 查询全部用户id
     *
     * @return
     */
    @PostMapping("/user/queryUserList")
    public R<TableDataInfo> queryUserList(@RequestBody UserDTO userDTO) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = userDTO.getPageNum();
        Integer pageSize = userDTO.getPageSize();
        String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
        Boolean reasonable = pageDomain.getReasonable();
        PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
        Long orgId = userDTO.getOrgId();
        if (Objects.isNull(orgId)) {
            orgId = tokenService.getLoginUser().getOrgId();
        }
        List<SysUser> sysUserList = platUserCenterFcService.queryUserIdList(orgId);
        return R.ok(getDataTable(sysUserList));
    }

    /**
     * 根据用户部门查询全部用户id
     *
     * @return
     */
    @PostMapping("/user/queryUserListByDeptId")
    public R<List<SysUser>> queryUserListByDeptId(@RequestBody UserDTO userDTO) {

        List<SysUser> sysUserList = platUserCenterFcService.queryUserIdList(userDTO.getOrgId());
        return R.ok(sysUserList);
    }

    /**
     * 根据用户部门查询全部用户id
     *
     * @return
     */
    @PostMapping("/user/queryUserListByDeptIds")
    public R<List<SysUserVO>> queryUserListByDeptId(@RequestBody SysUserVO sysUserVO) {
        List<SysUserVO> sysUserList = platUserCenterFcService.getUserInfoById(sysUserVO);
        return R.ok(sysUserList);
    }

    /**
     * 根据岗位集合列表查询全部用户id
     *
     * @return
     */
    @PostMapping("/user/queryUserListByPostIds")
    public R<List<SysOrgDeptUserVO>> queryUserListByPostIds(@RequestBody SysOrgDeptUserVO sysOrgDeptUserVO) {
        List<SysOrgDeptUserVO> sysUserList = platUserCenterFcService.queryUserListByPostIds(sysOrgDeptUserVO);
        return R.ok(sysUserList);
    }

    /**
     * 根据用户部门查询全部用户id
     *
     * @return
     */
    @PostMapping("/user/queryUserListByDeptName")
    public R<List<SysUser>> queryUserListByDeptName(@RequestParam("deptName") String deptName) {
        List<SysUser> sysUserList = platUserCenterFcService.queryUserListByDeptName(deptName);
        return R.ok(sysUserList);
    }

    /**
     * 根据用户类型查询全部用户id
     *
     * @return
     */
    @PostMapping("/user/queryUserIdListByUserType")
    public R<List<SysUser>> queryUserIdListByUserType(@RequestParam("userType") String userType) {
        if (StringUtils.isEmpty(userType)) {
            userType = tokenService.getLoginUser().getSysUser().getUserType();
        }
        List<SysUser> sysUserList = platUserCenterFcService.queryUserIdListByUserType(userType);
        return R.ok(sysUserList);
    }

    /**
     * 查询领导排班计划主列表
     */
    @GetMapping("/cal/leader/list")
    public R<TableDataInfo> queryLeaderPlan( Long orgId ) {
        CalLeaderPlanDTO calLeaderPlan = new CalLeaderPlanDTO();
        String today = DateUtil.initDateByDay();
        calLeaderPlan.setPlanStartDate(today);
        calLeaderPlan.setPlanEndDate(today);
        calLeaderPlan.setOrgId( orgId );
        startPage();
        List<CalLeaderPlan> list = platUserCenterFcService.selectCalLeaderPlanList(calLeaderPlan);
        return R.ok(getDataTable(list));
    }

    /**
     * 查询领导排班计划主列表
     */
    @PostMapping("/remote/cal/leader/list")
    public R<TableDataInfo> queryRemoteLeaderPlan( @RequestBody AirCargoRequest res ) {
        CalLeaderPlanDTO calLeaderPlan = new CalLeaderPlanDTO();
        String today = DateUtil.initDateByDay();
        calLeaderPlan.setPlanStartDate(today);
        calLeaderPlan.setPlanEndDate(today);
        calLeaderPlan.setOrgId( res.getOrgId() );
        Long orgId=calLeaderPlan.getOrgId( );
        if(Objects.isNull(orgId)){
            throw new RuntimeException( "orgId 查询参数缺失" );
        }else {
            //根据组织id查询下级组织的组织id
            // 查询下游组织信息-实际关系
            SysOrganizationRelation relationQuery = new SysOrganizationRelation();
            relationQuery.setParentId(orgId);
            relationQuery.setRelation( SysUserConstants.ORG_REAL);
            relationQuery.setIsDelete(SysUserConstants.N);
            List<SysOrganizationRelation> realOrgList = organizationRelationService.selectSysOrganizationRelationList(relationQuery);
            if (CollectionUtil.isNotEmpty(realOrgList)) {
                List< Long > collect=realOrgList.stream( ).map( SysOrganizationRelation::getOrgId )
                        .collect( Collectors.toList( ) );
                collect.add( orgId );
                calLeaderPlan.setOrgIdList( collect );
                calLeaderPlan.setOrgId(null);
            }
        }
        startPage();
        List<CalLeaderPlan> list = platUserCenterFcService.selectCalLeaderPlanList(calLeaderPlan);
        return R.ok(getDataTable(list));
    }


    /**
     * 排班查询接口：
     */
    @PostMapping("/cal/leader/selectLeaderPlan")
    public R<List<CalLeaderPlan>> selectLeaderPlan(@RequestBody CalLeaderPlanDTO dto) {
        Long orgId=dto.getOrgId( );
        if(Objects.isNull(orgId)){
            throw new RuntimeException( "orgId 查询参数缺失" );
        }else {
            //根据组织id查询下级组织的组织id
            // 查询下游组织信息-实际关系
            SysOrganizationRelation relationQuery = new SysOrganizationRelation();
            relationQuery.setParentId(orgId);
            relationQuery.setRelation( SysUserConstants.ORG_REAL);
            relationQuery.setIsDelete(SysUserConstants.N);
            List<SysOrganizationRelation> realOrgList = organizationRelationService.selectSysOrganizationRelationList(relationQuery);
            if (CollectionUtil.isNotEmpty(realOrgList)) {
                List< Long > collect=realOrgList.stream( ).map( SysOrganizationRelation::getOrgId )
                        .collect( Collectors.toList( ) );
                collect.add( orgId );
                dto.setOrgIdList( collect );
                dto.setOrgId(null);
            }
        }
        List<CalLeaderPlan> list = platUserCenterFcService.selectCalLeaderPlanList(dto);
        return R.ok(list);
    }

    /**
     * 获取部门用户列表
     */
    @GetMapping("/user/deptUser")
    public R<List<SysUser>> selectDeptUser(Long orgId, String keyword) {
        if (orgId == null) {
            return R.fail("缺少部门ID");
        }
        List<SysUser> list = platUserCenterFcService.selectDeptIdUsersWithKeyword(orgId, keyword);
        return R.ok(list);
    }


    /**
     * 分配用户-列表查询
     */
    @PostMapping("/getRoleUserList")
    public R<List<SysRoleVO>> getRoleUserList(@RequestBody com.platform.usercenter.domain.vo.SysRoleVO roleQuery) {
        List< SysRoleVO > userList=roleService.getUsersByRoleIds( roleQuery );
        return R.ok(userList);
    }
}
